package org.csu.jpetstore.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstore.domain.*;
import org.csu.jpetstore.service.CatalogService;
import org.csu.jpetstore.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    @Autowired
    private LogService logService;

//    @GetMapping("/main")
//    public String viewMain(HttpSession session, HttpServletRequest request){
//        return "catalog/main";
//    }

    //主界面进入大类
    @GetMapping("/category")
    public String viewCategory(@RequestParam String categoryId, Model model){
        if(categoryId!=null){
            Category category= catalogService.getCategory(categoryId);
            List<Product> productList= catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
            return "catalog/category";
        }
        return "cart/cart";
    }

    //从大类进入品种
    @GetMapping("/product")
    public String viewProduct(String productId, Model model){
        if(productId!=null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);
            return "catalog/product";
        }
        return "catalog/category";
    }

    //从品种进入具体
    @GetMapping("/item")
    public String viewItem(String itemId, HttpSession session,Model model){
        if(itemId!=null){
            try {
                Item item = catalogService.getItem(itemId);
                if(item == null) {
                    return "redirect:/catalog/product";
                }
                Product product = item.getProduct();
                if(product == null) {
                    return "redirect:/catalog/product";
                }
                String img = product.getDescription();
                if(img != null) {
                    String[] str = img.split("\"");
                    String imgsrc = str.length > 1 ? str[1] : "";
                    String[] str1 = img.split(">");
                    String des = str1.length > 1 ? str1[1] : "";
                    
                    model.addAttribute("imgsrc", imgsrc);
                    session.setAttribute("imgsrc", imgsrc);
                    model.addAttribute("description", des);
                    session.setAttribute("description", des);
                }
                model.addAttribute("item", item);
                session.setAttribute("item", item);
                model.addAttribute("product", product);
                session.setAttribute("product", product);
                return "catalog/item";
            } catch(Exception e) {
                return "redirect:/catalog/product";
            }
        }
        return "redirect:/catalog/product";
    }



//    @GetMapping("/completeSearch")
//    public void searchProducts(HttpServletResponse resp, String keyword, Model model) throws IOException {
//        System.out.println(keyword);
//        List<Product> productList=catalogService.getProductListByCategory(keyword);
//        resp.setContentType("text/json");
//        PrintWriter out=resp.getWriter();
//
//        JSONArray searchArray=new JSONArray();
//        for(int i=0;i<productList.size();i++){
//            JSONObject searchObject=new JSONObject();
//            searchObject.put("name",productList.get(i).getName());
//            searchArray.add(searchObject);
//        }
//        //转换成JSON字符串
//        String str=searchArray.toJSONString();
//        out.print(str);
//    }

//    @PostMapping("/searchProduct")
//    public void searchProduct(HttpServletResponse resp,HttpSession session, Model model,@RequestParam("keyword") String keyword) throws IOException {
//
//        System.out.println(keyword);
//        List<Product> productList = catalogService.getProductListByCategory(keyword);
//
//        //保存数据
//        session.setAttribute("keyword", keyword);
//        session.setAttribute("productList", productList);
//
//        resp.setContentType("application/json");
//        resp.setCharacterEncoding("UTF-8");
//        PrintWriter out = resp.getWriter();
//        Result result=new Result();
//
//        result.setCode(11);
//        result.setMsg("/catalog/searchProduct");
//        String str = JSON.toJSONString(result);
//        out.print(str);
//        out.flush();
//        out.close();
//    }
//
//    @GetMapping("/searchProduct")
//    public String viewSearchProduct(){
//        return "redirect:catalog/SearchProduct";
//    }
//
//    @PostMapping("showInformation")
//    public void showInformation(HttpServletResponse resp,HttpSession session,@RequestParam("categoryId") String categoryId) throws IOException{
//        resp.setContentType("application/json");//以json形式输出
//        PrintWriter out=resp.getWriter();
//        Result result1=new Result();
//        List<Product> productList = catalogService.getProductListByCategory(categoryId);
//        String productIdStr=String.format("%-12s","Product ID");
//        String nameStr=String.format("%-26s","Name");
//        String itemStr=String.format("%-12s","Item");
//        String response = itemStr+"   "+productIdStr+"   "+nameStr+"\n";
//        int i = 0;
//        while(i < productList.size()){
//            Product product = productList.get(i);
//            List<Item> itemList = catalogService.getItemListByProduct(product.getProductId());
//            for(int j=0;j<itemList.size();j++){
//                Item item=itemList.get(j);
//                String productId=String.format("%-12s", product.getProductId());
//                String name=String.format("%-26s",product.getName());
//                String itemId=String.format("%-10s",item.getItemId());
//                response += itemId+"   "+productId+"   "+name+"\n";;
//            }
//            i++;
//        }
//        String str = JSON.toJSONString(result1);
//        out.print(str);
//        out.flush();
//        out.close();
//    }
}
